home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / dynload_aix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  5.3 KB  |  205 lines

  1. /* Support for dynamic loading of extension modules */
  2.  
  3. #include <ctype.h>    /*  for isdigit()      */
  4. #include <errno.h>    /*  for global errno      */
  5. #include <string.h>    /*  for strerror()        */
  6. #include <stdlib.h>    /*  for malloc(), free()  */
  7. #include <sys/ldr.h>
  8.  
  9. #include "Python.h"
  10. #include "importdl.h"
  11.  
  12.  
  13. #ifdef AIX_GENUINE_CPLUSPLUS
  14. #include "/usr/lpp/xlC/include/load.h"
  15. #define aix_load loadAndInit
  16. #else
  17. #define aix_load load
  18. #endif
  19.  
  20.  
  21. extern char *Py_GetProgramName();
  22.  
  23. typedef struct Module {
  24.     struct Module *next;
  25.     void          *entry;
  26. } Module, *ModulePtr;
  27.  
  28. const struct filedescr _PyImport_DynLoadFiletab[] = {
  29.     {".so", "rb", C_EXTENSION},
  30.     {"module.so", "rb", C_EXTENSION},
  31.     {0, 0}
  32. };
  33.  
  34. static int
  35. aix_getoldmodules(modlistptr)
  36.     void **modlistptr;
  37. {
  38.     register ModulePtr       modptr, prevmodptr;
  39.     register struct ld_info  *ldiptr;
  40.     register char            *ldibuf;
  41.     register int             errflag, bufsize = 1024;
  42.     register unsigned int    offset;
  43.     char *progname = Py_GetProgramName();
  44.     
  45.     /*
  46.     -- Get the list of loaded modules into ld_info structures.
  47.     */
  48.     if ((ldibuf = malloc(bufsize)) == NULL) {
  49.         PyErr_SetString(PyExc_ImportError, strerror(errno));
  50.         return -1;
  51.     }
  52.     while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
  53.            && errno == ENOMEM) {
  54.         free(ldibuf);
  55.         bufsize += 1024;
  56.         if ((ldibuf = malloc(bufsize)) == NULL) {
  57.             PyErr_SetString(PyExc_ImportError, strerror(errno));
  58.             return -1;
  59.         }
  60.     }
  61.     if (errflag == -1) {
  62.         PyErr_SetString(PyExc_ImportError, strerror(errno));
  63.         return -1;
  64.     }
  65.     /*
  66.     -- Make the modules list from the ld_info structures.
  67.     */
  68.     ldiptr = (struct ld_info *)ldibuf;
  69.     prevmodptr = NULL;
  70.     do {
  71.         if (strstr(progname, ldiptr->ldinfo_filename) == NULL &&
  72.             strstr(ldiptr->ldinfo_filename, "python") == NULL) {
  73.             /*
  74.             -- Extract only the modules belonging to the main
  75.             -- executable + those containing "python" as a
  76.             -- substring (like the "python[version]" binary or
  77.             -- "libpython[version].a" in case it's a shared lib).
  78.             */
  79.             offset = (unsigned int)ldiptr->ldinfo_next;
  80.             ldiptr = (struct ld_info *)((unsigned int)
  81.                             ldiptr + offset);
  82.             continue;
  83.         }
  84.         if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
  85.             PyErr_SetString(PyExc_ImportError, strerror(errno));
  86.             while (*modlistptr) {
  87.                 modptr = (ModulePtr)*modlistptr;
  88.                 *modlistptr = (void *)modptr->next;
  89.                 free(modptr);
  90.             }
  91.             return -1;
  92.         }
  93.         modptr->entry = ldiptr->ldinfo_dataorg;
  94.         modptr->next  = NULL;
  95.         if (prevmodptr == NULL)
  96.             *modlistptr = (void *)modptr;
  97.         else
  98.             prevmodptr->next = modptr;
  99.         prevmodptr = modptr;
  100.         offset = (unsigned int)ldiptr->ldinfo_next;
  101.         ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset);
  102.     } while (offset);
  103.     free(ldibuf);
  104.     return 0;
  105. }
  106.  
  107. static int
  108. aix_bindnewmodule(newmoduleptr, modlistptr)
  109.     void *newmoduleptr;
  110.     void *modlistptr;        
  111. {
  112.     register ModulePtr modptr;
  113.  
  114.     /*
  115.     -- Bind the new module with the list of loaded modules.
  116.     */
  117.     for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next)
  118.         if (loadbind(0, modptr->entry, newmoduleptr) != 0)
  119.             return -1;
  120.     return 0;
  121. }
  122.  
  123. static void
  124. aix_loaderror(pathname)
  125.     char *pathname;
  126. {
  127.  
  128.     char *message[1024], errbuf[1024];
  129.     register int i,j;
  130.  
  131.     struct errtab { 
  132.         int errNo;
  133.         char *errstr;
  134.     } load_errtab[] = {
  135.         {L_ERROR_TOOMANY,    "too many errors, rest skipped."},
  136.         {L_ERROR_NOLIB,        "can't load library:"},
  137.         {L_ERROR_UNDEF,        "can't find symbol in library:"},
  138.         {L_ERROR_RLDBAD,
  139.          "RLD index out of range or bad relocation type:"},
  140.         {L_ERROR_FORMAT,    "not a valid, executable xcoff file:"},
  141.         {L_ERROR_MEMBER,
  142.          "file not an archive or does not contain requested member:"},
  143.         {L_ERROR_TYPE,        "symbol table mismatch:"},
  144.         {L_ERROR_ALIGN,        "text alignment in file is wrong."},
  145.         {L_ERROR_SYSTEM,    "System error:"},
  146.         {L_ERROR_ERRNO,        NULL}
  147.     };
  148.  
  149. #define LOAD_ERRTAB_LEN    (sizeof(load_errtab)/sizeof(load_errtab[0]))
  150. #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
  151.  
  152.     sprintf(errbuf, "from module %.200s ", pathname);
  153.  
  154.     if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
  155.         ERRBUF_APPEND(strerror(errno));
  156.         ERRBUF_APPEND("\n");
  157.     }
  158.     for(i = 0; message[i] && *message[i]; i++) {
  159.         int nerr = atoi(message[i]);
  160.         for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
  161.             if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
  162.             ERRBUF_APPEND(load_errtab[j].errstr);
  163.         }
  164.         while (isdigit(*message[i])) message[i]++ ; 
  165.         ERRBUF_APPEND(message[i]);
  166.         ERRBUF_APPEND("\n");
  167.     }
  168.     errbuf[strlen(errbuf)-1] = '\0';    /* trim off last newline */
  169.     PyErr_SetString(PyExc_ImportError, errbuf); 
  170.     return; 
  171. }
  172.  
  173.  
  174. dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
  175.                     const char *pathname, FILE *fp)
  176. {
  177.     dl_funcptr p;
  178.  
  179.     /*
  180.     -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
  181.     -- of the shared module unresolved. Thus we have to resolve them
  182.     -- explicitely with loadbind. The new module is loaded, then we
  183.     -- resolve its symbols using the list of already loaded modules
  184.     -- (only those that belong to the python executable). Get these
  185.     -- with loadquery(L_GETINFO).
  186.     */
  187.  
  188.     static void *staticmodlistptr = NULL;
  189.  
  190.     if (!staticmodlistptr)
  191.         if (aix_getoldmodules(&staticmodlistptr) == -1)
  192.             return NULL;
  193.     p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
  194.     if (p == NULL) {
  195.         aix_loaderror(pathname);
  196.         return NULL;
  197.     }
  198.     if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) {
  199.         aix_loaderror(pathname);
  200.         return NULL;
  201.     }
  202.  
  203.     return p;
  204. }
  205.